﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Microsoft.Office.OneNote;
using System.Xml.Linq;

namespace OneNoteFrameworkTests.PageContentTests
{
    [TestFixture]
    public class PageSizeTests
    {
        private const String NamespaceUri = "http://schemas.microsoft.com/office/onenote/2007/onenote";


        private static readonly XName Automatic = XName.Get("Automatic", NamespaceUri);
        private static readonly XName PageSize = XName.Get("PageSize", NamespaceUri);
        
        private static readonly XName Margins = XName.Get("Margins", NamespaceUri);
        private static readonly XName Dimensions = XName.Get("Dimensions", NamespaceUri);
        private static readonly XName Orientation = XName.Get("Orientation", NamespaceUri);
        
        [Test]
        public void SerializeAuto()
        {
            OneNotePageSize size = OneNotePageSize.Automatic;

            XElement elsize = OneNoteSchema.Serialize(size);
            Assert.IsNotNull(elsize);
            Assert.IsFalse(elsize.HasAttributes);
            Assert.IsTrue(elsize.HasElements);
            Assert.AreEqual(PageSize, elsize.Name);

            XElement elauto = elsize.Elements().First();
            Assert.IsNotNull(elauto);
            Assert.AreEqual(Automatic, elauto.Name);
            Assert.IsFalse(elauto.HasAttributes);
            Assert.IsFalse(elauto.HasElements);
            Assert.IsTrue(elauto.IsEmpty);
        }

        [Test]
        public void SerializeManual()
        {
            OneNotePageSize size = new OneNotePageSize();
            size.Orientation = OneNotePageOrientation.Landscape;
            size.Dimensions = new OneNotePageDimensions(500, 400); 
            size.Margins = new OneNotePageMargins(23, 42, 15.1, 22.6);
            
            XElement elsize = OneNoteSchema.Serialize(size);
            Assert.IsNotNull(elsize);
            Assert.IsFalse(elsize.HasAttributes);
            Assert.IsTrue(elsize.HasElements);
            Assert.AreEqual(PageSize, elsize.Name);

            XElement elorient = elsize.Element(Orientation);
            Assert.IsNotNull(elorient);
            Assert.IsTrue(elorient.HasAttributes);
            Assert.IsFalse(elorient.HasElements);

            XAttribute alandscape = elorient.Attribute(XName.Get("landscape"));
            Assert.IsNotNull(alandscape);
            Assert.IsNotEmpty(alandscape.Value);
            Assert.IsTrue(string.Equals(Boolean.TrueString, alandscape.Value, StringComparison.OrdinalIgnoreCase));

            XElement eldim = elsize.Element(Dimensions);
            Assert.IsNotNull(eldim);
            Assert.IsTrue(eldim.HasAttributes);
            Assert.IsFalse(eldim.HasElements);

            Assert.AreEqual(eldim.Attribute(XName.Get("width")).Value, "500");
            Assert.AreEqual(eldim.Attribute(XName.Get("height")).Value, "400");

            XElement elmarg = elsize.Element(Margins);
            Assert.IsNotNull(elmarg);
            Assert.IsTrue(elmarg.HasAttributes);
            Assert.IsFalse(elmarg.HasElements);

        }

        [Test]
        public void DeserializeAuto()
        {
            XDocument doc = XDocument.Parse(@"<one:PageSize xmlns:one="""+NamespaceUri+@""">
                                                <one:Automatic /></one:PageSize>");

            OneNoteObject obj = OneNoteSchema.Deserialize(doc.Root);
            Assert.IsNotNull(obj);
            Assert.IsInstanceOfType(typeof(OneNotePageSize), obj);

            OneNotePageSize size = (OneNotePageSize)(obj);
            Assert.IsTrue(size.IsAutomatic);
            Assert.AreEqual(OneNotePageSize.Automatic, size);
        }

        [Test]
        public void DeserializeManual()
        {
            XDocument doc = XDocument.Parse(@"<PageSize xmlns=""" + NamespaceUri + @""">
                                                 <Orientation landscape=""false"" />
                                                 <Margins right=""24"" bottom=""2.2345"" 
                                                          top=""11"" left=""33""
                                                 />
                                                 <Dimensions width=""100"" height=""200"" />
                                              </PageSize>");

            OneNotePageSize size = (OneNoteSchema.Deserialize(doc.Root) as OneNotePageSize);
            Assert.IsNotNull(size);
            Assert.IsFalse(size.IsAutomatic);
            Assert.AreNotEqual(OneNotePageSize.Automatic, size);
            Assert.AreEqual(OneNotePageOrientation.Portrait, size.Orientation);
            Assert.AreEqual(new OneNotePageMargins(33, 11, 24, 2.2345), size.Margins);
            Assert.AreEqual(new OneNotePageDimensions(100, 200), size.Dimensions);
        }

    }
}
